package com.wechat.controller;

import com.wechat.entity.WxMessageReq;
import com.wechat.entity.WxMessageRsp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


@RestController
@RequestMapping("/wx")
public class WxController {

    private static final Logger log = LoggerFactory.getLogger(WxController.class);

    /**
     * 服务器校验
     * @param signature
     * @param nonce
     * @param timestamp
     * @param echostr
     * @return
     */
    @RequestMapping(value = "/access", method = RequestMethod.GET)
    public String checkSignature(@RequestParam(name = "signature", required = false) String signature,
                                 @RequestParam(name = "nonce", required = false) String nonce,
                                 @RequestParam(name = "timestamp", required = false) String timestamp,
                                 @RequestParam(name = "echostr", required = false) String echostr) {
        log.info("请求参数：{},{},{},{}",signature, nonce, timestamp, echostr);
        // 通过检验signature对请求进行校验，若校验成功则原样返回echostr，表示接入成功，否则接入失败
        return echostr;
    }

    /**
     * 订阅、取消订阅、普通消息
     * @param wxMessageReq
     * @return
     */
    @RequestMapping(value = "/access", produces = {MediaType.TEXT_XML_VALUE},consumes = {MediaType.TEXT_XML_VALUE},
            method = RequestMethod.POST)
    @ResponseBody
    public Object pushEventsPost(@RequestBody WxMessageReq wxMessageReq) {
        log.info("请求参数:{}", wxMessageReq);
        String event = wxMessageReq.getEvent();
        if("event".equals(event)){
            return "success";
        }
        WxMessageRsp wxMessageRsp = new WxMessageRsp();
        wxMessageRsp.setToUserName(wxMessageReq.getFromUserName());
        wxMessageRsp.setFromUserName(wxMessageReq.getToUserName());
        wxMessageRsp.setCreateTime(new Date().getTime());
        wxMessageRsp.setMsgType(wxMessageReq.getMsgType());
        if("text".equals(wxMessageReq.getMsgType())){
            //回复文本消息
            wxMessageRsp.setContent(wxMessageReq.getContent());
        }
        if("image".equals(wxMessageReq.getMsgType())){
            //回复图片消息
            WxMessageRsp.Image image = new WxMessageRsp.Image();
            image.setMediaId(wxMessageReq.getMediaId());
            wxMessageRsp.setImage(image);
        }
        if("voice".equals(wxMessageReq.getMsgType())){
            //回复语音消息
            WxMessageRsp.Voice voice = new WxMessageRsp.Voice();
            voice.setMediaId(wxMessageReq.getMediaId());
            wxMessageRsp.setVoice(voice);
        }
        if("video".equals(wxMessageReq.getMsgType())){
            //回复视频消息
            WxMessageRsp.Video video = new WxMessageRsp.Video();
            video.setMediaId(wxMessageReq.getMediaId());
            video.setTitle("视频");
            video.setDescription("视频描述");
            wxMessageRsp.setVideo(video);
        }
        return wxMessageRsp;
    }

}
